<?php
/**
 * Users controller class
 *
 */

class CronController extends Admin_Controller
{


	public function init()
    {
    	parent::init();
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {
    	die();
    }


	// GHOST cleanner!
    public function ghostcleannerAction()
    {
		Zend_Registry::get('db')->delete('articles', array("is_ghost" => 1));
		Zend_Registry::get('db')->delete('accounts', array("is_ghost" => 1));
		//Zend_Registry::get('db')->delete('events', array("is_ghost" => 1));
		//Zend_Registry::get('db')->delete('forum', array("is_ghost" => 1));
		//Zend_Registry::get('db')->delete('gallery', array("is_ghost" => 1));
		//Zend_Registry::get('db')->delete('groups', array("is_ghost" => 1));
		Zend_Registry::get('db')->delete('objects', array("is_ghost" => 1));
    	die('done');
    }

/*
	// Tags count for Articles
    public function tagsartcountAction()
    {
    	$data = Zend_Registry::get('db')->select()
					->from('tags_to_articles', array("id_tag", "count" =>"count(id_tag)"))
					->group('id_tag')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		Zend_Registry::get('db')->update('tags', array("count_articles" => 0));

		foreach($data as $k => $v){
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $v['id_tag']);
			$where = Zend_Registry::get('db')->update('tags', array("count_articles" => $v['count']), $where);
		}

    	die('done');
    }
 * 
 */
    // Set gone for past auctions
    public function setgoneAction()
    {
    	$data = Zend_Registry::get('db')->select()
					->from('auction_lots', array("id", "id_status"))
                                        ->where('auction_lots.is_ghost = 0')
                                        ->where('auction_lots.is_removed = 0')
                                        ->where('auction_lots.is_gone = 0')
                                        ->where('auction_lots.id_status = 2 or auction_lots.id_status = 12')                                        
                                        ->joinLeft('auction', "auction_lots.id_auction = auction.id",array('start_date' => 'auction.start_date'))
                                        ->where('start_date < CURRENT_DATE')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

        
		//Zend_Registry::get('db')->update('tags', array("count_articles" => 0));

		foreach($data as $k => $v){
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $v['id']);
			$where = Zend_Registry::get('db')->update('auction_lots', array("is_gone" => "1"), $where);
		}
    	die('done');
    }
/*
    // Cities count for accounts
    public function citiesaccountscountAction()
    {
    	$data = Zend_Registry::get('db')->select()
					->from('accounts', array("id_city", "count" =>"count(id_city)"))
					->group('id_city')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		Zend_Registry::get('db')->update('cities', array("count_accounts" => 0));

		foreach($data as $k => $v){
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $v['id_city']);
			$where = Zend_Registry::get('db')->update('cities', array("count_accounts" => $v['count']), $where);
		}

    	die('done');
    }

    // Members count for country
    public function memcountcountryAction()
    {
    	$data = Zend_Registry::get('db')->select()
					->from('accounts', array("id_country", "count" =>"count(id_country)"))
					->group('id_country')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		Zend_Registry::get('db')->update('countries', array("count_accounts" => 0));

		foreach($data as $k => $v){
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $v['id_country']);
			$where = Zend_Registry::get('db')->update('countries', array("count_accounts" => $v['count']), $where);
		}

    	die('done');
    }

    // Members count for groups
    public function memcountgroupsAction()
    {
    	$data = Zend_Registry::get('db')->select()
					->from('groups_members', array("id_group", "count" =>"count(id_group)"))
					->where('is_accepted')
					->group('id_group')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		Zend_Registry::get('db')->update('groups', array("count_accounts" => 0));

		foreach($data as $k => $v){
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $v['id_group']);
			$where = Zend_Registry::get('db')->update('groups', array("count_accounts" => $v['count']), $where);
		}

    	die('done');
    }

     public function searchindexAction()
    {
    	Zend_Registry::get('db')->query("TRUNCATE TABLE search");

    	$data = Zend_Registry::get('db')->select()
					->from('products')
					->join('categories', 'categories.id = products.id_cat', array('url'))
					->where('products.is_active')
					->where('products.is_ghost = 0')
					->where('products.is_removed = 0')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);


		foreach($data as $k => $v){
			$d = array();
			$d['add_date'] = time();

			$d['id_table'] = $v['id'];
			$d['table'] = 'products';

			$d['title_ru'] = $v['title_ru'];
			$d['title_ua'] = $v['title_ua'];

			$d['text_ru'] = $v['title_ru'].' '.$v['subtitle_ru'].' '.$v['desc_ru'].' '.$v['body_ru'];
			//$d['text_en'] = $v['title_en'].' '.$v['subtitle_en'].' '.$v['desc_en'].' '.$v['body_en'];
			$d['text_ua'] = $v['title_ua'].' '.$v['subtitle_ua'].' '.$v['desc_ua'].' '.$v['body_ua'];


			$d['url'] = $v['url'].'/'.$v['idt'];

			Zend_Registry::get('db')->insert('search', $d);
		}

		// Articles //

        	$data = Zend_Registry::get('db')->select()
					->from('articles')
					->joinLeft('categories', 'categories.id = articles.id_cat', array('url'))
					->where('articles.is_active')
					->where('articles.is_ghost = 0')
					->where('articles.is_removed = 0')
					->where('articles.id_lang > 0')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		$lang[1] = 'ru';
		$lang[2] = 'ua';
		$lang[3] = 'en';


		foreach($data as $k => $v){
			$d = array();
			$d['add_date'] = time();
			$d['id_table'] = $v['id'];
			$d['table'] = 'articles';

			if(!isset($lang[intval($v['id_lang'])])) continue;

			$d['title_'.$lang[$v['id_lang']]] = $v['title'];
			$d['text_'.$lang[$v['id_lang']]] = $v['title'].' '.$v['desc'].' '.$v['body'];

			switch($v['type'])
			{
			    case 1:
			        $d['url'] = '/uslugi/'.$v['idt'];
			        break;
			    case 2:
			        $d['url'] = '/perspektivnie-razrabotki/'.$v['idt'];
			        break;
			    default:
			        $d['url'] = $v['url'].'/'.$v['idt'];
			        break;
			}

			Zend_Registry::get('db')->insert('search', $d);
		}

		// Categories //
        $data = Zend_Registry::get('db')->select()
					->from('categories')
					->where('site = ?', 'client')
					->where('is_active = 1')
					->where('is_show = 1')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);


		foreach($data as $k => $v){
			$d = array();
			$d['add_date'] = time();
			$d['id_table'] = $v['id'];
			$d['table'] = 'categories';

			$d['title_ru'] = $v['title_ru'];
			$d['title_en'] = $v['title_en'];
			$d['title_ua'] = $v['title_ua'];

			$d['text_ru'] = $v['title_ru'].' '.$v['desc_ru'].' '.$v['text_ru'];
			$d['text_en'] = $v['title_en'].' '.$v['desc_en'].' '.$v['text_en'];
			$d['text_ua'] = $v['title_ua'].'  '.$v['desc_ua'].' '.$v['text_ua'];

			$d['url'] = $v['url'];

			Zend_Registry::get('db')->insert('search', $d);
		}

		Zend_Registry::get('db')->query("REPAIR TABLE search QUICK");



    	die('done');
    }

*
 * 
 */


}
